package com.rdzn.mall.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rdzn.mall.base.common.Result;
import com.rdzn.mall.base.common.ResultCode;
import com.rdzn.mall.base.exception.Asserts;
import com.rdzn.mall.center.entity.UmsMember;
import com.rdzn.mall.center.entity.UmsProductCollect;
import com.rdzn.mall.center.service.IUmsProductCollectService;
import com.rdzn.mall.portal.domain.PmsPortalProductDetail;
import com.rdzn.mall.portal.domain.ProductCollect;
import com.rdzn.mall.portal.service.IProductService;
import com.rdzn.mall.portal.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
* @Description: 商品收藏
* @Author: jeecg-boot
* @Date:   2020-09-18
* @Version: V1.0
*/
@Api(tags="商品收藏")
@RestController
@RequestMapping("/collect")
@Slf4j
public class MUmsProductCollectController extends JeecgController<UmsProductCollect, IUmsProductCollectService> {
    @Autowired
    private IUmsProductCollectService productCollectService;
    @Autowired
    private IProductService productService;
    @Autowired
    private IUserService userService;

    /**
     * 收藏
     *
     * @param id
     * @return
     */
    @AutoLog(value = "收藏")
    @ApiOperation(value="收藏", notes="收藏")
    @GetMapping(value = "/addordelete")
    public Result<?> collect(@RequestParam(name="id",required=true) Integer id) {
        PmsPortalProductDetail detail = productService.getDetail(id);
        if(detail==null) {
            return Result.error("未找到对应数据");
        }
        UmsMember member = userService.getCurrentMember();
        if (member == null){
            return Result.error(ResultCode.UNAUTHORIZED);
        }
        detail.setCollect(productCollectService.collect(member.getId(), id));
        return Result.ok(detail);
    }

    /**
     * 刷新收藏，如果没有收藏添加收藏，已经收藏的刷新收藏时间
     *
     * @param collect
     * @return
     */
    @AutoLog(value = "收藏")
    @ApiOperation(value="收藏", notes="收藏")
    @PostMapping(value = "/reCollect")
    public Result<?> reCollect(@RequestBody ProductCollect collect) {
//        PmsPortalProductDetail detail = productService.(id);
//        if(detail==null) {
//            return Result.error("未找到对应数据");
//        }
        UmsMember member = userService.getCurrentMember();
        if (member == null){
            return Result.error(ResultCode.UNAUTHORIZED);
        }
        return Result.ok(productCollectService.collects(member.getId(), collect.getIds()));
    }

    /**
     * 判断当前用户是否收藏商品
     *
     * @param productId
     * @return
     */
    @AutoLog(value = "判断当前用户是否收藏商品")
    @ApiOperation(value="判断当前用户是否收藏商品", notes="判断当前用户是否收藏商品")
    @GetMapping(value = "/isCollected")
    public Result<?> isCollected(@RequestParam(name="productId",required=true) Integer productId) {
        UmsProductCollect umsProductCollect = productCollectService.getOne(userService.getCurrentMember().getId(), productId);
        if(umsProductCollect==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(umsProductCollect);
    }

   /**
    * 分页列表查询
    *
    * @param umsProductCollect
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "商品收藏-分页列表查询")
   @ApiOperation(value="商品收藏-分页列表查询", notes="商品收藏-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(UmsProductCollect umsProductCollect,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       UmsMember member = userService.getCurrentMember();
       if (member == null){
           Asserts.fail(ResultCode.UNAUTHORIZED);
       }
       umsProductCollect.setUserId(member.getId());
       QueryWrapper<UmsProductCollect> queryWrapper = QueryGenerator.initQueryWrapper(umsProductCollect, req.getParameterMap());
       queryWrapper.orderByDesc("update_time");
       Page<UmsProductCollect> page = new Page<UmsProductCollect>(pageNo, pageSize);
       IPage<UmsProductCollect> pageList = productCollectService.page(page, queryWrapper);
       IPage<ProductCollect> data = pageList.convert(productCollect -> {
           ProductCollect temp = new ProductCollect();
           BeanUtils.copyProperties(productCollect, temp);
           temp.setProduct(productService.getDetail(temp.getProductId()));
           return temp;
       });
       return Result.ok(data);
   }

   /**
    *   添加
    *
    * @param umsProductCollect
    * @return
    */
   @AutoLog(value = "商品收藏-添加")
   @ApiOperation(value="商品收藏-添加", notes="商品收藏-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody UmsProductCollect umsProductCollect) {
       productCollectService.save(umsProductCollect);
       return Result.ok("添加成功！");
   }

   /**
    *  编辑
    *
    * @param umsProductCollect
    * @return
    */
   @AutoLog(value = "商品收藏-编辑")
   @ApiOperation(value="商品收藏-编辑", notes="商品收藏-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody UmsProductCollect umsProductCollect) {
       productCollectService.updateById(umsProductCollect);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "商品收藏-通过id删除")
   @ApiOperation(value="商品收藏-通过id删除", notes="商品收藏-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       productCollectService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "商品收藏-批量删除")
   @ApiOperation(value="商品收藏-批量删除", notes="商品收藏-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.productCollectService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "商品收藏-通过id查询")
   @ApiOperation(value="商品收藏-通过id查询", notes="商品收藏-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       UmsProductCollect umsProductCollect = productCollectService.getById(id);
       if(umsProductCollect==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(umsProductCollect);
   }
}
